﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using FirmaTransportowa.Presenters;

namespace FirmaTransportowa
{
    public class LoginPresenter
    {
        private static LoginForm view;

        public LoginPresenter()
        {
            view = new LoginForm(this);
        }

        public LoginForm getView() {
            return view;
        }

        public static void showLoginView() 
        {
            view.Show();
            view.passwordBox.Text = "";
            view.loginBox.Text = "";
        }

        public void login() {
            String login = view.loginBox.Text;
            String password = view.passwordBox.Text;

            User user = User.getByLogin(login);

            if (login == "") {
                view.errorLogin.Text = "Podaj nazwę użytkownika";
                view.loginLabel.ForeColor = Color.Red;
            }
            else if (password == "") {
                view.errorPassword.Text = "Podaj hasło";
                view.passwordLabel.ForeColor = Color.Red;
            }
            else if (user == null)
            {
                view.errorLogin.Text = "Użytkownik nie istnieje";
                view.loginLabel.ForeColor = Color.Red;
            }
            else if (user.password != password)
            {
                view.errorPassword.Text = "Nieprawidłowe hasło";
                view.passwordLabel.ForeColor = Color.Red;
            }
            else
            {
                view.Hide();

                switch (user.getOccupationId())
                {
                    case 1:
                        new DriverPanelPresenter(user);
                        break;
                    case 2:
                        new DispatcherPanelPresenter(user);
                        break;
                    case 3:
                        new CashierPanelPresenter(user);
                        break;
                    case 4:
                        new AdminPanelPresenter(user);
                        break;
                    case 5:
                        new StatisticsPanelPresenter(user);
                        break;
                    case 6:
                        new ManagerPanelPresenter(user);
                        break;
                }
            }
        }
    }
}
